package com.accp.ssmoa.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.accp.ssmoa.pojo.Menu;
import com.accp.ssmoa.pojo.Role;
import com.accp.ssmoa.pojo.RoleAndMenuKey;
import com.accp.ssmoa.service.MenuService;
import com.accp.ssmoa.service.RoleService;
import com.accp.ssmoa.service.RoleMenuService;
import com.alibaba.fastjson.JSONObject;

@Controller()
@Scope("prototype")
@RequestMapping("/admin/role")
public class RoleController {
	
	@Autowired
	private RoleService service;
	
	
	@Resource(name="roleMenuServiceImpl")
	private RoleMenuService roleMenuService;
	
	
	@Resource(name="menuServiceimpl")
	private MenuService menuService;
	
	
	
	
	//根据主键查询
	public Role getById(Integer id) {
		return service.findById(id);
	}
	/**
	 * 分页
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	
	@RequestMapping(value="/RolePages",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Map<String, Object> showRolePageMsg(@RequestBody JSONObject jsonObj)  {
		
		//取出前台传过来的封装在json格式里面的数据
		Integer pageSize = jsonObj.getIntValue("pageSize");  //每页显示数据条数
		Integer startRow = jsonObj.getIntValue("startRow");  	//当前页码
		Integer totalsize=service.getTotalsize();//总数据数

		Map<String, Object> map = new HashMap<String, Object>(); 
		
		List<Role> departments=service.getPageRole(startRow, pageSize);
		
		//total表示数据总数，rows表示需要显示的数据。必须按照这种格式返回才行。
		map.put("total",totalsize);
		map.put("rows", departments);
		
		return map;
	}
	
	

	
	
	/**
	 * 添加
	 * @param role
	 * @param model
	 * @return
	 */
	@RequestMapping("/addRoles")
	@ResponseBody
	public Map<String,Boolean> addRoles(Role role,Model model) {
		
		Map<String,Boolean> result=new HashMap<String, Boolean>();
		
		service.addRole(role);
		
		//添加成功返回的验证信息
		result.put("success", true);

		
		return result;
	}
	
	

	
	/**
	 * 修改
	 * @param map
	 * @param role
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/updateRoles", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Boolean> updateRoles(Map<String,Object>map,Role role,Model model) {
		//map.put("roles", service.findById(id));
		
		Map<String,Boolean> result=new HashMap<String, Boolean>();
		
		service.updateRole(role);
		
		//添加成功返回的验证信息
		result.put("success", true);
		
		return result;
	}

	/**
	 * 实现授权
	 * @return
	 */
	@RequestMapping("/changeRoleAuth")
	@ResponseBody
	public Map<String,Boolean> delegating(@RequestParam("roleid")Integer roleid,
			@RequestParam(value = "menuIds[]") String[] menuIds) {
		
		Map<String,Boolean> result=new HashMap<String, Boolean>();
		
		List<RoleAndMenuKey> list=new ArrayList<RoleAndMenuKey>();
		
		RoleAndMenuKey roleMenu;
		
		for (String idString : menuIds) {

			roleMenu=new RoleAndMenuKey();
			roleMenu.setRoleId(roleid);
			roleMenu.setMenuId(Integer.parseInt(idString));
			
			list.add(roleMenu);
		}
		
		//先删除角色所有权限
		roleMenuService.deleteByRoleId(roleid);
		
		//批量增加为角色选择的权限
		int a=roleMenuService.insertByList(list);
		
		//根据批量新增返回受影响的数据条数来判断操作是否成功
		if(a>0) {
			result.put("success", true);
		}else {
			result.put("success", false);
		}
		
		
		return result;
	}
	
	
    //显示菜单信息树形图
	@RequestMapping("/getAllMenuTree")
	@ResponseBody
	public  List<Object>  getAllMenuTree(){
		
		List<Object> menuTree=menuService.getMenuTree();
		
		
		return menuTree;
	}
	
	
    //根据角色id获取已有权限
	@RequestMapping("/getRoleAuthByRoleid")
	@ResponseBody
	public  List<Menu>  getRoleAuthByRoleid(@RequestParam("roleid")Integer roleid){
		
		List<Menu> menus=menuService.getMenusByRoleid(roleid);
		
		
		return menus;
	}
	
	

	
	
	
	/*需要进行权限控制的操作，增删改以及授权*/

	
	/**
	 * 跳转到添加页面
	 */
	@RequestMapping("/save")
	public String save(HttpServletRequest request) {
		
		//存储面包屑导航的值进request作用域
		request.setAttribute("pageTitle","角色管理");
		
		request.setAttribute("url","/admin/role/getListPage");
		
		request.setAttribute("nowBread","添加角色");		
		
		
		return "role/addRole";
	}
	
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	//redirect:    重定向
	@RequestMapping("/delete")
	@ResponseBody
	public Map<String,Object> delete(@RequestParam("roleids[]") String[] roleids) {
		
		Map<String,Object> result=new HashMap<String, Object>();
		List<Integer> ids =new ArrayList<Integer>();
		
		for (String idString : roleids) {
			
			
			ids.add(Integer.parseInt(idString));
			
			if(Integer.parseInt(idString)==1) {
				result.put("success", false);
				result.put("message", "含有无法删除角色");
				
				return result;
			}
		}
		
		int a=0;
		
		
		a=service.deleteRoles(ids);
		
		if(a>0) {
			result.put("success", true);
			result.put("message", "删除成功");		
		}else {
			result.put("success", false);
			result.put("message", "删除失败");
		}

		
		return result;
		
	}
	
	
	//跳转到修改页面
	@RequestMapping("/update")
	public String update(@RequestParam("id")Integer id, Map<String,Object>map,Model model,
			HttpServletRequest request) {	
		
		map.put("roles", service.findById(id)); 

		//存储面包屑导航的值进request作用域
		request.setAttribute("pageTitle","角色管理");
		request.setAttribute("url","/admin/role/getListPage");
		request.setAttribute("nowBread","修改角色");	

		return "role/edit";
	}
	
	
	//获取弹出选择权限的树形图
	@RequestMapping("/getGiveAuthPage")
    public String getGiveAuthPage(@RequestParam("roleid")Integer roleid,
    		HttpServletRequest request){
    	
		
		request.setAttribute("roleid", roleid);
		
		
    	return ("role/giveAuth");
    }	
	
	
	/**
	 * 初始化角色页面
	 * @param model
	 * @param map
	 * @return
	 */
	@RequestMapping("/getListPage")
	public String getListPage(Model model,Map<String,Object> map,HttpServletRequest request) {
		//List<Role> list = service.findAll();
		map.put("rolese", service.findAll());
		
		//存储面包屑导航的值进request作用域
		request.setAttribute("pageTitle","角色管理");
		
		request.setAttribute("url","/admin/role/getListPage");
		
		request.setAttribute("nowBread","管理");
		
		return "role/LoadAllRoles";
	}
	
}
